package icasue.reflect.adaptor;

import icasue.reflect.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2021/9/29 下午2:33
 * @UpdateDate:
 * @Description:
 * 该类属于适配类，本次抽离base模块，
 * 但仍要适配base模块的部分内容，例如二次代理后的单例实例的获取等.
 */
public abstract class AdaptorManager {

    // 存储该模块适配类下的单例实例.
    private static final Map<Class<?>,Object> instances = new HashMap<>();

    static {
        instances.put(AdaptorManager.ApplicationTouch.class, new AdaptorManager.ApplicationTouch());
        instances.put(AdaptorManager.CommonCheck.class, new AdaptorManager.CommonCheck());
    }

    @NotNull
    protected final static <Impl extends AdaptorModel> Impl getInst(Class<Impl > implType){
        Impl inst = null;
        try {
            inst = (Impl) instances.get(implType);
            if(inst == null){
                inst = implType.newInstance();
                synchronized (instances){
                    if(!instances.containsKey(implType)){
                        instances.put(implType,inst);
                    }
                }
            }
        }catch (Throwable e){  }
        return inst;
    }

    /**
     * 适配base模块 [icasue.base.touch.ApplicationTouch]类.
     */
    protected static class ApplicationTouch extends AdaptorModel {
        @Override
        public String classReflectAble() {
            return "icasue.base.touch.ApplicationTouch";
        }
    }

    /**
     * 适配base模块的 [icasue.base.utils.check.CommonCheck] 类.
     */
    protected static class CommonCheck extends AdaptorModel {
        @Override
        public String classReflectAble() {
            return "icasue.base.utils.check.CommonCheck";
        }
    }
}
